package com.sandking.db.jedis;

import java.util.ArrayList;
import com.sandking.tools.SK_Plus;
import com.sandking.tools.SK_Jedis;
import com.sandking.db.bean.Yhxl;
import java.util.List;
import java.util.Set;
import com.sandking.db.dao.YhxlDao;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Jedis;

public class YhxlJedis{
	/**
	 * 根据( id ) 查询
	 */
	public static Yhxl getById(int id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Yhxl yhxl = null;
		try{
			String key = "Yhxl_Object";
			String field = SK_Plus.b("id:",id).e();
			yhxl = Yhxl.createForJson(jedis.hget(key,field));
			if(yhxl == null){
				yhxl = YhxlDao.getById(id);
				if(yhxl!=null){
					loadCache(yhxl);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return yhxl;
	}
	
	/**
	 * 根据( 用户_id ) 查询
	 */
	public static List<Yhxl> getByYh_id(int yh_id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		List<Yhxl> yhxls = new ArrayList<Yhxl>();
		try{
			String key = "Yhxl_Index";
			String field = SK_Plus.b(key,"yh_id:",yh_id).e();
			Set<String> setStr = jedis.smembers(field);
			if(setStr!=null){
				key = "Yhxl_Object";
				String[] fieldArray = (String[]) setStr.toArray();
				List<String> jsons = jedis.hmget(key,fieldArray);
				yhxls = Yhxl.createForJson(jsons);
			}else{
				yhxls = YhxlDao.getByYh_id(yh_id);
				if(yhxls!=null && !yhxls.isEmpty()){
					loadCaches(yhxls);
				}
			}
			
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		return yhxls;
	}
	
	/**
	 * 根据( 是否有效  用户_id ) 查询
	 */
	public static List<Yhxl> getBySfyxYh_id(boolean sfyx, int yh_id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		List<Yhxl> yhxls = new ArrayList<Yhxl>();
		try{
			String key = "Yhxl_Index";
			String field = SK_Plus.b(key,"sfyx, yh_id:",sfyx, yh_id).e();
			Set<String> setStr = jedis.smembers(field);
			if(setStr!=null){
				key = "Yhxl_Object";
				String[] fieldArray = (String[]) setStr.toArray();
				List<String> jsons = jedis.hmget(key,fieldArray);
				yhxls = Yhxl.createForJson(jsons);
			}else{
				yhxls = YhxlDao.getBySfyxYh_id(sfyx, yh_id);
				if(yhxls!=null && !yhxls.isEmpty()){
					loadCaches(yhxls);
				}
			}
			
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		return yhxls;
	}
	

	public static void loadCache(Yhxl yhxl){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			loadCache(yhxl,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 加载一个缓存
	 */
	public static void loadCache(Yhxl yhxl,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Yhxl_Object";
		String field = SK_Plus.b("id:",yhxl.getId()).e();
		String data = yhxl.toJson();
		p.hset(key,field,data);
		
		String primaryKey = String.valueOf(yhxl.getId());
		key = "Yhxl_Index";
		// ---------------------- 聚集索引 ----------------------
		field = SK_Plus.b(key,"yh_id:",yhxl.getYh_id()).e();
		p.sadd(field, primaryKey);
		
		field = SK_Plus.b(key,"sfyx, yh_id:",yhxl.getSfyx(),yhxl.getYh_id()).e();
		p.sadd(field, primaryKey);
		
	}
	
	/**
	 * 加载一组缓存
	 */
	public static void loadCaches(List<Yhxl> yhxls){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Yhxl yhxl : yhxls){
				loadCache(yhxl,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static void clearCache(Yhxl yhxl){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			clearCache(yhxl,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 清空一个缓存
	 */
	public static void clearCache(Yhxl yhxl,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Yhxl_Object";
		String field = SK_Plus.b("id:",yhxl.getId()).e();
		p.hdel(key,field);
		
		key = "Yhxl_Index";
		// ---------------------- 聚集索引 ----------------------
		String primaryKey = String.valueOf(yhxl.getId());
		field = SK_Plus.b(key,"yh_id:",yhxl.getYh_id()).e();
		p.srem(field, primaryKey);
		
		field = SK_Plus.b(key,"sfyx, yh_id:",yhxl.getSfyx(),yhxl.getYh_id()).e();
		p.srem(field, primaryKey);
		
	}
	/**
	 * 清空一组缓存
	 */
	public static void clearCaches(List<Yhxl> yhxls){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Yhxl yhxl : yhxls){
				clearCache(yhxl,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static Yhxl insert(Yhxl yhxl){
		yhxl = YhxlDao.insert(yhxl);
		if(yhxl!=null){
			loadCache(yhxl);
		}
    	return yhxl;
    }
    
    public static Yhxl update(Yhxl yhxl){
    	yhxl = YhxlDao.update(yhxl);
    	if(yhxl!=null){
			loadCache(yhxl);
		}
    	return yhxl;
    }
    
    public static boolean delete(Yhxl yhxl){
    	boolean bool = YhxlDao.delete(yhxl);
    	if(bool){
    		clearCache(yhxl);
    	}
    	return bool;
    }
}